home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 5 / Example 5.7 / object.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-06-29  |  2.1 KB  |  85 lines

  1. #include "object.h"
  2.  
  3. std::vector<MESH*> objectMeshes;
  4.  
  5. HRESULT LoadObjectResources(IDirect3DDevice9* Device)
  6. {
  7.     MESH *mech1 = new MESH("Objects/mech1.x", Device);
  8.     objectMeshes.push_back(mech1);
  9.  
  10.     MESH *mech2 = new MESH("Objects/mech2.x", Device);
  11.     objectMeshes.push_back(mech2);
  12.  
  13.     MESH *mech3 = new MESH("Objects/mech3.x", Device);
  14.     objectMeshes.push_back(mech3);
  15.  
  16.     return S_OK;
  17. }
  18.  
  19. void UnloadObjectResources()
  20. {
  21.     for(int i=0;i<objectMeshes.size();i++)
  22.         objectMeshes[i]->Release();
  23.  
  24.     objectMeshes.clear();
  25. }
  26.  
  27. //////////////////////////////////////////////////////////////////////////////
  28. //                            OBJECT CLASS                                    //
  29. //////////////////////////////////////////////////////////////////////////////
  30.  
  31. OBJECT::OBJECT()
  32. {
  33.     m_type = 0;
  34. }
  35.  
  36. OBJECT::OBJECT(int t, D3DXVECTOR3 pos, D3DXVECTOR3 rot)
  37. {
  38.     m_type = t;
  39.  
  40.     for(int i=0;i<3;i++)
  41.     {
  42.         m_meshInstances[i].SetPosition(pos);
  43.         m_meshInstances[i].SetRotation(rot);
  44.         m_meshInstances[i].SetScale(D3DXVECTOR3(1.0f, 1.0f, 1.0f));
  45.         m_meshInstances[i].SetMesh(objectMeshes[m_type + i]);
  46.     }
  47.  
  48.     m_BBox = m_meshInstances[1].GetBoundingBox();
  49. }
  50.  
  51. void OBJECT::Render(CAMERA *camera, long &noFaces, int &noObjects)
  52. {
  53.     //If camera == NULL, Render High Res Mesh
  54.     if(camera == NULL)    
  55.     {
  56.         m_meshInstances[0].Render();
  57.         noFaces += m_meshInstances[0].m_pMesh->m_pMesh->GetNumFaces();
  58.         noObjects++;
  59.     }
  60.     else
  61.     {
  62.         if(!camera->Cull(m_BBox))        //Cull objects
  63.         {
  64.             //Distance from objects to camera
  65.             float dist = D3DXVec3Length(&(m_meshInstances[0].m_pos - camera->m_eye));
  66.             noObjects++;
  67.  
  68.             if(dist < 50.0f)        //Close to the Camera
  69.             {
  70.                 m_meshInstances[0].Render();    //Render High Res
  71.                 noFaces += m_meshInstances[0].m_pMesh->m_pMesh->GetNumFaces();
  72.             }
  73.             else if(dist < 100.0f)    //Average distance from Camera
  74.             {
  75.                 m_meshInstances[1].Render();    //Render Medium Res mesh
  76.                 noFaces += m_meshInstances[1].m_pMesh->m_pMesh->GetNumFaces();
  77.             }
  78.             else                    //Far from camera
  79.             {
  80.                 m_meshInstances[2].Render();    //Render Low Res Mesh
  81.                 noFaces += m_meshInstances[2].m_pMesh->m_pMesh->GetNumFaces();
  82.             }
  83.         }
  84.     }
  85. }